package com.situ.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.admin.bean.Goods;
import com.situ.admin.mapper.GoodsMapper;
import com.situ.admin.mapper.GoodsPicturreMapper;
import com.situ.admin.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Autowired
	private GoodsPicturreMapper goodsPicturreMapper;

	@Override
	public Goods add(Goods goods) {
		goodsMapper.insert(goods);
		goodsPicturreMapper.batchInsert(goods.getId(), goods.getPictures());

		return goodsMapper.selectByPrimaryKey(goods.getId());
	}

	@Override
	public Goods remove(Integer id) {
		Goods goods = goodsMapper.selectByPrimaryKey(id);

		if (goods.getStatus() == 0) {
			goods.setStatus(1); // 软删除
		} else if (goods.getStatus() == 1) {
			goods.setStatus(0);
		}

		goodsMapper.updateByPrimaryKey(goods);
		return goods;
	}

	@Override
	@Transactional
	public Goods edit(Goods goods) {
		goodsMapper.updateByPrimaryKey(goods);

		return goodsMapper.selectByPrimaryKey(goods.getId());
	}

	@Override
	public PageInfo getByPage(Integer page, Integer limit, Goods goods) {
		if (page == null) {
			page = 1;
		}
		if (limit == null) {
			List list = goodsMapper.select(goods);
			limit = list.size();
		}
		PageHelper.startPage(page, limit);
		List list = goodsMapper.select(goods);
		PageInfo pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

}
